<template>
  <a-modal
    title="启动"
    :width="840"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="16">
          <a-col class="gutter-row" :span="12">
            <a-form-item
              label="id"
              v-show="false"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['id']"
                :disabled="true"
              />
            </a-form-item>
            <a-form-item
              label="所属部门"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-tree-select
                showSearch
                placeholder="选择部门"
                allowClear
                :treeData="treeData"
                treeDefaultExpandAll
                treeNodeFilterProp="title"
                v-decorator="['departId']"
              />
            </a-form-item>
            <a-form-item
              label="拟稿人"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <!--<a-input
                :disabled="showable"
                v-decorator="['draftMan']" />-->
              <a-select
                showSearch
                :disabled="showable"
                placeholder="请选择"
                v-decorator="['draftMan']"
                :options="userList"
                :filterOption="filterOption"
                optionFilterProp="children"
              />
            </a-form-item>
            <a-form-item
              label="拟稿单位"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-tree-select
                showSearch
                placeholder="选择拟稿单位"
                allowClear
                :treeData="treeData"
                treeDefaultExpandAll
                treeNodeFilterProp="title"
                v-decorator="['draftingUnit']"/>
            </a-form-item>
            <a-form-item
              label="文件字号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                :disabled="showable"
                v-decorator="['issuedNo']" />
            </a-form-item>
            <a-form-item
              label="文件标题"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                :disabled="showable"
                v-decorator="['issuedTitle']" />
            </a-form-item>
          </a-col>
          <a-col class="gutter-row" :span="12">
            <a-form-item
              label="文件类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                :disabled="showable"
                v-decorator="['issuedType']" />
            </a-form-item>
            <a-form-item
              label="附件"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                :disabled="showable"
                v-decorator="['otherFile']" />
            </a-form-item>
            <a-form-item
              label="发送单位"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-tree-select
                showSearch
                placeholder="选择发送单位s"
                allowClear
                :treeData="treeData"
                treeDefaultExpandAll
                treeNodeFilterProp="title"
                v-decorator="['sendDepart']"/>
            </a-form-item>
            <a-form-item
              label="年份"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                :disabled="showable"
                v-decorator="['year']" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
    <template slot="footer" >
      <div>
        <a-button key="cancel" @click="handleCancel">取消</a-button>
        <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
      </div>
    </template>
  </a-modal>
</template>

<script>
import { createIssued } from '@/api/matters/issued'
import { FormMixin } from '@/mixins/FormMixin'
import { treeDataTranslate } from '@/utils/util'
import { fetchTree } from '@/api/sys/dept'
import { dropList } from '@/api/sys/user'
export default {
  name: 'InstanceStart',
  mixins: [FormMixin],
  data () {
    return {
      title: '启动流程',
      confirmLoading: false,
      form: this.$form.createForm(this),
      imgUrl: '',
      treeData: [],
      userList: []
    }
  },
  methods: {
    // 入口
    edit (id) {
      this.visible = true
      const { form: { setFieldsValue, resetFields } } = this
      resetFields()
      this.$nextTick(() => {
        setFieldsValue({ id })
      })
      fetchTree().then(res => {
        this.treeData = treeDataTranslate(res.data, 'deptId', 'name')
      })
      dropList().then(res => {
        this.userList = res.data
      })
    },
    // 确认
    handleOk () {
      this.form.validateFields(async (err, values) => {
        values.issuedType = Number(values.issuedType)
        if (!err) {
          this.confirmLoading = true
          try {
            await createIssued(values)
            setTimeout(() => {
              this.confirmLoading = false
              this.$emit('ok')
              this.visible = false
            }, 1500)
          } catch (e) {
            this.$message.error(e.msg)
            this.confirmLoading = false
          }
        }
      })
    },
    filterOption (input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    handleCancel () {
      this.visible = false
      setTimeout(() => {
        this.addable = false
        this.showable = false
        this.editable = false
      }, 200)
    }
  }

}
</script>

<style scoped>

</style>
